package com.lovo.controller;

import com.google.gson.Gson;
import com.lovo.bean.Goods;
import com.lovo.service.IGoodsService;
import com.lovo.service.serviceImpl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "GoodsServlet",urlPatterns = "/Goods.do")
public class GoodsServlet extends HttpServlet {
    IGoodsService is = new GoodsServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if (method == null || method.equalsIgnoreCase("getAll")){
            List<Goods> all = is.getAll();
//            System.out.println("all = " + all);
            response.getWriter().write(new Gson().toJson(all));
        }else if (method.equalsIgnoreCase("GoodsAdd")){
            String goodsName = request.getParameter("goodsName");
            String goodsType = request.getParameter("goodsType");
            String goodsMaterial = request.getParameter("goodsMaterial");
            String goodsDogtype = request.getParameter("goodsDogtype");
            String goodsExdogtype = request.getParameter("goodsExdogtype");
            String goodsPackage = request.getParameter("goodsPackage");
            String goodsFlavor = request.getParameter("goodsFlavor");
            String goodsFunction = request.getParameter("goodsFunction");
            String goodsPlace = request.getParameter("goodsPlace");
            String goodsDate = request.getParameter("goodsDate");
            String goodsLasting = request.getParameter("goodsLasting");
            String goodsFeature = request.getParameter("goodsFeature");
            String goodsPrice = request.getParameter("goodsPrice");

            Goods goods = new Goods();
            goods.setGoodsName(goodsName);
            goods.setGoodsType(goodsType);
            goods.setGoodsMaterial(goodsMaterial);
            goods.setGoodsDogtype(goodsDogtype);
            goods.setGoodsExdogtype(goodsExdogtype);
            goods.setGoodsPackage(goodsPackage);
            goods.setGoodsFlavor(goodsFlavor);
            goods.setGoodsFunction(goodsFunction);
            goods.setGoodsPlace(goodsPlace);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date =null;
            try {
                date = simpleDateFormat.parse(goodsDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            goods.setGoodsDate(date);
            goods.setGoodsLasting(goodsLasting);
            goods.setGoodsFeature(goodsFeature);
            goods.setGoodsPrice(goodsPrice);

            boolean b = is.GoodsAdd(goods);
            if (b){
                response.getWriter().write("true");
            }else {
                response.getWriter().write("false");
            }
        }else if (method.equalsIgnoreCase("delete")){
            String goodsId = request.getParameter("goodsId");
            boolean b = is.deleteById(Integer.parseInt(goodsId));
            if (b){
                response.getWriter().write("true");
            }else {
                response.getWriter().write("false");
            }
        }else if (method.equalsIgnoreCase("selectById")){
            String goodsId = request.getParameter("goodsId");
            Goods goods = is.selectById(Integer.parseInt(goodsId));
            System.out.println(goods);
            response.getWriter().write(new Gson().toJson(goods));
        }else if (method.equalsIgnoreCase("updateGoods")){
            String goodsId = request.getParameter("goodsId");
            int id = Integer.parseInt(goodsId);
            String goodsName = request.getParameter("goodsName");
            String goodsType = request.getParameter("goodsType");
            String goodsMaterial = request.getParameter("goodsMaterial");
            String goodsDogtype = request.getParameter("goodsDogtype");
            String goodsExdogtype = request.getParameter("goodsExdogtype");
            String goodsPackage = request.getParameter("goodsPackage");
            String goodsFlavor = request.getParameter("goodsFlavor");
            String goodsFunction = request.getParameter("goodsFunction");
            String goodsPlace = request.getParameter("goodsPlace");
            String goodsDate = request.getParameter("goodsDate");
            String goodsLasting = request.getParameter("goodsLasting");
            String goodsFeature = request.getParameter("goodsFeature");
            String goodsPrice = request.getParameter("goodsPrice");

            Goods goods = new Goods();
            goods.setGoodsId(id);
            goods.setGoodsName(goodsName);
            goods.setGoodsType(goodsType);
            goods.setGoodsMaterial(goodsMaterial);
            goods.setGoodsDogtype(goodsDogtype);
            goods.setGoodsExdogtype(goodsExdogtype);
            goods.setGoodsPackage(goodsPackage);
            goods.setGoodsFlavor(goodsFlavor);
            goods.setGoodsFunction(goodsFunction);
            goods.setGoodsPlace(goodsPlace);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(" ");
            Date date =null;
            try {
                date = simpleDateFormat.parse(goodsDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            goods.setGoodsDate(date);
            goods.setGoodsLasting(goodsLasting);
            goods.setGoodsFeature(goodsFeature);
            goods.setGoodsPrice(goodsPrice);

            boolean b = is.updateByPrimaryKeySelective(goods);
            if (b){
                response.getWriter().write("true");
            }else {
                response.getWriter().write("false");
            }
        }
     }
}
